package com.larry.mr2.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    //输出路径
    String lp = "C:\\Users\\lishanglai\\IdeaProjects\\map-reduce\\src\\main\\resources\\output2\\lp.txt";
    String op = "C:\\Users\\lishanglai\\IdeaProjects\\map-reduce\\src\\main\\resources\\output2\\op.txt";

    FSDataOutputStream lpout = null;
    FSDataOutputStream opout = null;
    public LogRecordWriter(TaskAttemptContext job) throws IOException {
        FileSystem fs = FileSystem.get(job.getConfiguration());
        lpout = fs.create(new Path("lp"));
        opout = fs.create(new Path("op"));
    }


    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String log = key.toString();
        if (log.contains("larry")){
            lpout.writeUTF(log + "\n");
        }else {
            opout.writeUTF(log + "\n");
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(lpout);
        IOUtils.closeStream(opout);
    }
}
